package io.github.talelin.latticy.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.ProjectDTO;
import io.github.talelin.latticy.model.ProjectDO;
import io.github.talelin.latticy.service.ProjectService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/cms/project")
@PermissionModule("项目管理")
public class IProjectController {

    @Autowired
    private ProjectService projectService;

    @PostMapping()
    @PermissionMeta(value = "创建项目")
    @GroupRequired
    public CreatedVO create(@RequestBody @Validated ProjectDTO dto) {
        projectService.add(dto);
        return new CreatedVO<>();
    }

    @PutMapping("/{id}")
    @PermissionMeta(value = "编辑项目")
    @GroupRequired
    public UpdatedVO update(@PathVariable @Positive Long id,
                       @RequestBody @Validated ProjectDTO dto) {
        projectService.updated(id, dto);
        return new UpdatedVO<>();
    }

    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除项目")
    @GroupRequired
    public DeletedVO delete(@PathVariable @Positive Long id) {
        projectService.get(id);
        projectService.getBaseMapper().deleteById(id);
        return new DeletedVO<>();
    }

    @GetMapping("/{id}")
    @LoginRequired
    public ProjectDO get(@PathVariable @Positive Long id) {
        return projectService.getProject(id);
    }

    /**
     * 列表
     * @param page
     * @param count
     * @return
     */
    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO page(@RequestParam(required = false, defaultValue = "0")
                     @Min(value = 0, message = "{page.number.min}") Integer page,
                     @RequestParam(required = false, defaultValue = "10")
                     @Min(value = 1, message = "{page.count.min}")
                     @Max(value = 30, message = "{page.count.max}") Integer count) {
        Page<ProjectDO> pager = new Page<>(page, count);
        QueryWrapper<ProjectDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(ProjectDO::getCreateTime);
        IPage<ProjectDO> paging = projectService.getBaseMapper().selectPage(pager, wrapper);
        return new PageResponseVO<>(paging.getTotal(), paging.getRecords(), paging.getCurrent(), paging.getSize());
    }

    @GetMapping("/all")
    @LoginRequired
    public List<ProjectDO> getAll() {
        return projectService.getBaseMapper().selectList(null);
    }
}


